package com.chc.web.user.handler.user;

import com.chc.config.jwt.JwtUtils;
import com.chc.enums.UserTypeEnum;
import com.chc.utils.Msg;
import com.chc.web.live_user.entity.LiveUser;
import com.chc.web.live_user.service.LiveUserService;
import com.chc.web.menu.entity.MakeMenuTree;
import com.chc.web.menu.entity.Menu;
import com.chc.web.menu.entity.RouterVO;
import com.chc.web.menu.service.MenuService;
import com.chc.web.user.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chc
 * @Date: 2025/01/15/15:16
 * @Description:
 */
@Component
public class UserTypeHandler implements LoginTypeHandler{

    @Autowired
    private MenuService menuService;

    @Autowired
    private LiveUserService liveUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    //注入token实现类
    @Autowired
    private JwtUtils jwtUtils;

    @Override
    public UserTypeEnum getUserType() {
        return UserTypeEnum.USER;
    }

    @Override
    public Msg menu(String username) {
        //查询业主的权限信息
        LiveUser liveUser = liveUserService.loadUser(username);
        List<Menu> menuList = menuService.getMenuListByUserIdForLiveUser(liveUser.getUserId());
        //只需要获取目录和菜单
        List<Menu> collect = menuList.stream().filter(item -> item != null && !item.getType().equals("2")).collect(Collectors.toList());
        //组装路由数据
        List<RouterVO> list = MakeMenuTree.makeRouter(collect,0L);
        return Msg.success("查询成功",list);
    }

    @Override
    public Msg resetPwd(ChangePassword user) {
        LiveUser liveUser = liveUserService.getById(user.getUserId());
        //原来的密码
        String oldPassword = liveUser.getPassword();
        boolean encode = passwordEncoder.matches(user.getOldPassWord(), oldPassword);
        if (!encode){
            return  Msg.error("旧密码错误");
        }
        LiveUser liveUser1 = new LiveUser();
        liveUser1.setUserId(user.getUserId());
        liveUser1.setPassword(passwordEncoder.encode(user.getNewPassWord()));
        boolean b = liveUserService.updateById(liveUser1);
        if (b){
            return Msg.success("密码修改成功!");
        }
        return Msg.error("密码修改失败");
    }

    @Override
    public Msg Info(User user) {
        //根据业主id查询该登录用户
        LiveUser liveUser = liveUserService.getById(user.getUserId());
        //用户信息实体类
        UserInfo userInfo = new UserInfo();
        //获取用户id
        userInfo.setId(liveUser.getUserId());
        //获取用户姓名
        userInfo.setName(liveUser.getUsername());
        //获取用户头像
        userInfo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        //根据用户id查询,区分查询是哪一个
        //查询用户权限信息
        List<Menu> menuList = menuService.getMenuListByUserIdForLiveUser(liveUser.getUserId());
        List<String> collect = menuList.stream().filter(item -> item != null).map(item -> item.getMenuCode())
                .filter(item -> item != null).collect(Collectors.toList());
        //转成数组
        String[] strings = collect.toArray(new String[collect.size()]);
        userInfo.setRoles(strings);
        //返回成功的信息给前端
        return Msg.success("获取用户信息成功",userInfo);
    }

    @Override
    public Msg login(LoginParm parm, Authentication authenticate) {
        LiveUser user = (LiveUser) authenticate.getPrincipal();
        //如果用户存在，生成token返回给前端
        String token = jwtUtils.generateToken(parm.getUsername(),parm.getUserType());
        //获取生成的token的过期时间
        Long expireTime =  jwtUtils.getExpireTime(token,jwtUtils.getSecret());
        LoginResult result = new LoginResult();
        result.setUserId(user.getUserId());
        result.setToken(token);
        result.setExpireTime(expireTime);
        return Msg.success("登录成功",result);
    }

}
